<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态
check_login();
$db = db_connect();
// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
$date = isset($_GET['date']) ? trim($_GET['date']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : 1;
// 每页显示的记录数
$per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $per_page;
// 构建查询条件
$where = "1=1";
$params = [];
if ($search) {
    $where .= " AND (c.name LIKE :search OR cc.name LIKE :search)";
    $params[':search'] = "%{$search}%";
}
if ($status >= 0) {
    $where .= " AND c.status = :status";
    $params[':status'] = $status;
}
if ($category_id > 0) {
    $where .= " AND c.category_id = :category_id";
    $params[':category_id'] = $category_id;
}
if ($date) {
    $where .= " AND DATE(c.course_date) = :date";
    $params[':date'] = $date;
}
// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM courses c 
               LEFT JOIN course_categories cc ON c.category_id = cc.id 
               WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);

// 获取课程列表
$sql = "SELECT c.*, cc.name as category_name, co.name as coach_name 
        FROM courses c 
        LEFT JOIN course_categories cc ON c.category_id = cc.id 
        LEFT JOIN coaches co ON c.coach_id = co.id 
        WHERE {$where} 
        ORDER BY c.created_at DESC
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$courses = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 获取课程分类
$category_sql = "SELECT * FROM course_categories WHERE status = 1";
$category_stmt = $db->query($category_sql);
$categories = $category_stmt->fetchAll();
// 难度等级映射
$difficulty_levels = [
    1 => ['name' => '初级', 'badge' => 'badge-success'],
    2 => ['name' => '中级', 'badge' => 'badge-warning'],
    3 => ['name' => '高级', 'badge' => 'badge-danger']
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>团课管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
    .filter-bar {
        background: #fff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }
    
    .search-bar {
        display: flex;
        gap: 20px;
        align-items: center;
        flex-wrap: wrap;
        position: relative;
    }
    
    .search-bar input[type="text"] {
        flex: 1;
        min-width: 200px;
        padding: 10px 15px;
        padding-left: 40px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        transition: border-color 0.3s;
        background: #f8f9fa;
    }
    
    .search-bar input[type="text"]:focus {
        outline: none;
        border-color: #3498db;
        background: #fff;
    }
    
    /* 搜索框图标 */
    .search-bar::before {
        content: '\f002';
        font-family: 'Font Awesome 5 Free';
        font-weight: 900;
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        color: #6c757d;
        font-size: 14px;
    }
    
    .search-bar select {
        padding: 10px 35px 10px 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: #f8f9fa url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'%3E%3Cpath fill='%23666' d='M0 2l4 4 4-4z'/%3E%3C/svg%3E") no-repeat right 15px center;
        font-size: 14px;
        min-width: 120px;
        cursor: pointer;
        appearance: none;
        -webkit-appearance: none;
        transition: all 0.3s;
    }
    
    .search-bar select:focus {
        outline: none;
        border-color: #3498db;
        background-color: #fff;
    }
    
    .search-bar input[type="date"] {
        padding: 9px 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: #f8f9fa;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;
        min-width: 150px;
    }
    
    .search-bar input[type="date"]:focus {
        outline: none;
        border-color: #3498db;
        background: #fff;
    }
    
    .search-bar button {
        padding: 10px 25px;
        background: #3498db;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        transition: background 0.3s;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    
    .search-bar button:hover {
        background: #2980b9;
        transform: translateY(-1px);
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    
    .search-bar button i {
        font-size: 14px;
    }
    
    /* 响应式调整 */
    @media (max-width: 768px) {
        .search-bar {
            flex-direction: column;
            align-items: stretch;
        }
        
        .search-bar input[type="text"],
        .search-bar select,
        .search-bar input[type="date"] {
            width: 100%;
        }
        
        .search-bar button {
            width: 100%;
            justify-content: center;
        }
    }
    .course-image {
        width: 80px;
        height: 60px;
        object-fit: cover;
        border-radius: 4px;
        cursor: pointer;
    }
    .badge {
        display: inline-block;
        padding: 5px 10px;
        border-radius: 4px;
        font-size: 12px;
    }
    .capacity-info {
        display: flex;
        align-items: center;
        gap: 5px;
    }
    .progress-bar {
        width: 100px;
        height: 6px;
        background: #eee;
        border-radius: 3px;
        overflow: hidden;
    }
    .progress {
        height: 100%;
        background: #4CAF50;
        transition: width 0.3s;
    }
    .badge-success {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }
    .badge-warning {
        background: #fff3cd;
        color: #856404;
        border: 1px solid #ffeeba;
    }
    .badge-danger {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>团课课程管理</h2>
                </div>
                <div class="header-right">
                    <a href="course_add.php" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 添加课程
                    </a>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <input type="text" name="search" placeholder="搜索课程名称或教练..." 
                               value="<?php echo htmlspecialchars($search); ?>">
                        
                        <select name="category_id" onchange="document.getElementById('searchForm').submit()">
                            <option value="0">全部分类</option>
                            <?php foreach ($categories as $category): ?>
                            <option value="<?php echo $category['id']; ?>" 
                                    <?php echo $category_id == $category['id'] ? 'selected' : ''; ?>>
                                <?php echo $category['name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                        
                        <select name="status" onchange="document.getElementById('searchForm').submit()">
                            <option value="-1">全部状态</option>
                            <option value="1" <?php echo $status === 1 ? 'selected' : ''; ?>>正常</option>
                            <option value="0" <?php echo $status === 0 ? 'selected' : ''; ?>>已取消</option>
                        </select>
                        
                        <input type="date" name="date" value="<?php echo $date; ?>" 
                               onchange="document.getElementById('searchForm').submit()"
                               placeholder="选择日期">
                        
                        <button type="submit"><i class="fas fa-search"></i> 搜索</button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>封面</th>
                                <th>课程名称</th>
                                <th>分类</th>
                                <th>教练</th>
                                <th>上课时间</th>
                                <th>时长</th>
                                <th>预约情况</th>
                                <th>难度</th>
                                <th>价格</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($courses as $course): ?>
                            <tr>
                                <td><?php echo $course['id']; ?></td>
                                <td>
                                    <img src="../api/user/<?php echo htmlspecialchars($course['cover_image']); ?>" 
                                         class="course-image" 
                                         onclick="showImage(this.src)"
                                         alt="课程封面">
                                </td>
                                <td><?php echo htmlspecialchars($course['name']); ?></td>
                                <td><?php echo htmlspecialchars($course['category_name']); ?></td>
                                <td><?php echo htmlspecialchars($course['coach_name']); ?></td>
                                <td>
                                    <?php 
                                        echo date('Y-m-d', strtotime($course['course_date'])) . '<br>';
                                        echo date('H:i', strtotime($course['start_time'])) . '-' . 
                                             date('H:i', strtotime($course['end_time']));
                                    ?>
                                </td>
                                <td><?php echo $course['duration']; ?>分钟</td>
                                <td>
                                    <div class="capacity-info">
                                        <span><?php echo $course['booked_count']; ?>/<?php echo $course['capacity']; ?></span>
                                        <div class="progress-bar">
                                            <div class="progress" style="width: <?php echo ($course['booked_count'] / $course['capacity']) * 100; ?>%"></div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <?php
                                    $difficulty_class = '';
                                    switch ($course['difficulty']) {
                                        case 1:
                                            $difficulty_class = 'badge-success';
                                            $difficulty_text = '初级';
                                            break;
                                        case 2:
                                            $difficulty_class = 'badge-warning';
                                            $difficulty_text = '中级';
                                            break;
                                        case 3:
                                            $difficulty_class = 'badge-danger';
                                            $difficulty_text = '高级';
                                            break;
                                    }
                                    ?>
                                    <span class="badge <?php echo $difficulty_class; ?>">
                                        <?php echo $difficulty_text; ?>
                                    </span>
                                </td>
                                <td>¥<?php echo number_format($course['price'], 2); ?></td>
                                <td>
                                    <span class="badge <?php echo $course['status'] ? 'badge-success' : 'badge-danger'; ?>">
                                        <?php echo $course['status'] ? '正常' : '已取消'; ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="course_edit.php?id=<?php echo $course['id']; ?>" 
                                       class="btn-icon" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <?php if ($course['status']): ?>
                                    <a href="javascript:void(0)" 
                                       onclick="cancelCourse(<?php echo $course['id']; ?>)"
                                       class="btn-icon text-warning" title="取消课程">
                                        <i class="fas fa-ban"></i>
                                    </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?php if ($page > 1): ?>
                    <a href="?page=1<?php echo $search ? '&search='.urlencode($search) : ''; ?><?php echo $category_id ? '&category_id='.$category_id : ''; ?><?php echo $status != -1 ? '&status='.$status : ''; ?><?php echo $date ? '&date='.$date : ''; ?>">&laquo; 首页</a>
                    <a href="?page=<?php echo ($page-1); ?><?php echo $search ? '&search='.urlencode($search) : ''; ?><?php echo $category_id ? '&category_id='.$category_id : ''; ?><?php echo $status != -1 ? '&status='.$status : ''; ?><?php echo $date ? '&date='.$date : ''; ?>">上一页</a>
                    <?php endif; ?>

                    <?php
                    // 显示当前页码附近的页码
                    $start_page = max(1, $page - 2);
                    $end_page = min($total_pages, $page + 2);

                    for ($i = $start_page; $i <= $end_page; $i++):
                    ?>
                        <a href="?page=<?php echo $i; ?><?php echo $search ? '&search='.urlencode($search) : ''; ?><?php echo $category_id ? '&category_id='.$category_id : ''; ?><?php echo $status != -1 ? '&status='.$status : ''; ?><?php echo $date ? '&date='.$date : ''; ?>" 
                           class="<?php echo $page == $i ? 'active' : ''; ?>">
                            <?php echo $i; ?>
                        </a>
                    <?php endfor; ?>

                    <?php if ($page < $total_pages): ?>
                    <a href="?page=<?php echo ($page+1); ?><?php echo $search ? '&search='.urlencode($search) : ''; ?><?php echo $category_id ? '&category_id='.$category_id : ''; ?><?php echo $status != -1 ? '&status='.$status : ''; ?><?php echo $date ? '&date='.$date : ''; ?>">下一页</a>
                    <a href="?page=<?php echo $total_pages; ?><?php echo $search ? '&search='.urlencode($search) : ''; ?><?php echo $category_id ? '&category_id='.$category_id : ''; ?><?php echo $status != -1 ? '&status='.$status : ''; ?><?php echo $date ? '&date='.$date : ''; ?>">末页 &raquo;</a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal" onclick="this.style.display='none'">
        <img id="modalImage" class="modal-content">
    </div>

    <script>
    function showImage(src) {
        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');
        modal.style.display = "block";
        modalImg.src = src;
    }

    function cancelCourse(id) {
        if (confirm('确定要取消这个课程吗？')) {
            window.location.href = `course_cancel.php?id=${id}`;
        }
    }
    </script>
</body>
</html> 